﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XSTDZ.ERP.Application.DataContracts;
using AutoMapper;
using XSTDZ.ERP.Domain.Models;
using XSTDZ.ERP.Core.Repositories;
using XSTDZ.ERP.Core.Generator;

namespace XSTDZ.ERP.Application.Implements
{
    public class CustomerServiceImpl : ApplicationServiceBase, ICustomerService
    {
        IRepository<ClientInfo> _clientInfoRepository;
        IRepository<ClientLink> _clientLinkRepository;
        public CustomerServiceImpl(IRepositoryContext repositoryContext, IRepository<ClientInfo> clientInfoRepository, IRepository<ClientLink> clientLinkRepository)
            : base(repositoryContext)
        {
            this._clientInfoRepository = clientInfoRepository;
            this._clientLinkRepository=clientLinkRepository;
        }
        #region 客户服务


        public void AddClientInfo(ClientInfoDto clientInfoDto)
        {
            var clientInfo = Mapper.Map<ClientInfoDto, ClientInfo>(clientInfoDto);
            clientInfo.Id = (Guid)new SequentialIdentityGenerator().Next;
            clientInfo.Init();
            this._clientInfoRepository.Add(clientInfo);
            this.Context.Commit();
        }

        public void UpdateClientInfo(ClientInfoDto clientInfoDto)
        {
            var clientInfo = Mapper.Map<ClientInfoDto, ClientInfo>(clientInfoDto);
            this._clientInfoRepository.Update(clientInfo);
            this.Context.Commit();
        }

        public void ConvertClientInfoDeleteState(Guid clientId)
        {
            var clientInfo = this._clientInfoRepository.GetByKey(clientId);
            clientInfo.SetDeleteState();
            this._clientInfoRepository.Update(clientInfo);
            this.Context.Commit();
        }

        public ClientInfoDto GetClientInfo(Guid clientId)
        {
            var clientInfo = this._clientInfoRepository.GetByKey(clientId);
            return Mapper.Map<ClientInfo, ClientInfoDto>(clientInfo);
        }

        public IEnumerable<ClientInfoDto> GetClientInfo(int pageIndex, int pageSize)
        {
            var clientInfos = this._clientInfoRepository.GetAll(pageIndex, pageSize);
            return Mapper.Map<IEnumerable<ClientInfo>, IEnumerable<ClientInfoDto>>(clientInfos);
        }

        public IEnumerable<ClientInfoDto> GetClientInfo(int pageIndex, int pageSize, string clientName)
        {
            var clientInfos = this._clientInfoRepository.GetAll(pageIndex, pageSize, c => c.ClientName.Contains(clientName) || c.ShortName.Contains(clientName));
            return Mapper.Map<IEnumerable<ClientInfo>, IEnumerable<ClientInfoDto>>(clientInfos);
        }

        public IEnumerable<ClientInfoDto> GetClientInfo(int pageIndex, int pageSize, int? ProvincialID, int? CityID)
        {
            throw new NotImplementedException();
        }
        #endregion
        #region 客户联系人服务
        public void AddClientLink(ClientLinkDto clientLinkDto)
        {
            var clientLink = Mapper.Map<ClientLinkDto, ClientLink>(clientLinkDto);
            clientLink.Id = (Guid)new SequentialIdentityGenerator().Next;
            clientLink.Init();
            this._clientLinkRepository.Add(clientLink);
            this.Context.Commit();
        }

        public void UpdateClientLink(ClientLinkDto clientLinkDto)
        {
            var clientLink = Mapper.Map<ClientLinkDto, ClientLink>(clientLinkDto);
            this._clientLinkRepository.Update(clientLink);
            this.Context.Commit();
        }

        public void ConvertClientLinkDeleteState(Guid clientLinkId)
        {
            var clientLink = this._clientLinkRepository.GetByKey(clientLinkId);
            clientLink.SetDeleteState();
            this._clientLinkRepository.Update(clientLink);
            this.Context.Commit();
        }

        public ClientLinkDto GetClientLink(Guid clientLinkId)
        {
            var clientLink = this._clientLinkRepository.GetByKey(clientLinkId);
            return Mapper.Map<ClientLink, ClientLinkDto>(clientLink);
        }

        public IEnumerable<ClientLinkDto> GetClientLink(int pageIndex, int pageSize)
        {
            var clientLinks = this._clientLinkRepository.GetAll(pageIndex, pageSize);
            return Mapper.Map<IEnumerable<ClientLink>, IEnumerable<ClientLinkDto>>(clientLinks);
        }

        public IEnumerable<ClientLinkDto> GetClientLink(int pageIndex, int pageSize, string clientLinkName)
        {
            var clientLinks = this._clientLinkRepository.GetAll(pageIndex, pageSize,c=>c.LinkManNmae.Contains(clientLinkName));
            return Mapper.Map<IEnumerable<ClientLink>, IEnumerable<ClientLinkDto>>(clientLinks);
        }

        public IEnumerable<ClientLinkDto> GetClientLink(int pageIndex, int pageSize, int? ProvincialID, int? CityID)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
